<?php
$counter = 0;

$newArr = array();

foreach ($data as $item) {
	
	if (array_key_exists($item['hotelRoomId'], $newArr)) {
		$newArr[$item['hotelRoomId']]['dateField' . $item['hotelDateGroupId']] = $item['price'];
	} else {
		$newArr[$item['hotelRoomId']] = array(
			'id'    => $item['hotelRoomId'],
			'title' => $item['title'],
			'dateField' . $item['hotelDateGroupId'] => $item['price'],
			'action' => 'icon-delete-item'
		);
	}
}

foreach ($newArr as &$item) {
	foreach ($columns as $col) {
		$findKey = 'dateField' . $col->id;
		if (!array_key_exists($findKey, $item)) {
			$item[$findKey] = '';
		}
	}
}

function arraySort($a, $b)
{
	$_sort  = $GLOBALS['_sortStrIntoFunction'];
	$_order = $GLOBALS['_orderStrIntoFunction'];
	
	if (floatval($a[$_sort]) == floatval($b[$_sort])) {
		return 0; 
	}
	
	if (strtolower($_order) == 'asc') {
		return (floatval($a[$_sort]) < floatval($b[$_sort])) ? -1 : 1;
	} else {
		return (floatval($a[$_sort]) < floatval($b[$_sort])) ? 1 : -1;
	}
}

if ($sort != 'title') {
	$GLOBALS['_sortStrIntoFunction']  = $sort;
	$GLOBALS['_orderStrIntoFunction'] = $order;
	usort($newArr, 'arraySort');
}

$obj = new stdClass();
$obj->data = array();

foreach ($newArr as $entity) {
	$counter++;
	array_push($obj->data, (object)$entity);
}

$obj->totalCount = $counter; 

echo json_encode($obj);